package controller.common;

import domain.entity.base.UserInfo;
import net.sf.json.JSONArray;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import util.WebContextUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class CommonController {
    private static final Logger logger = LoggerFactory.getLogger(CommonController.class);

//    @RequestMapping(value = "/statisticalInfor")
//    public String statisticalInfor(HttpServletRequest httpRequest, String code, String state) {
//        String token = "";
//        try {
//            logger.info("=============================================================" + code + "=================================================================================");
//            //获取token地址
//            String getTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww0e7be2a6a0200924&corpsecret=mfL3J4oMtcksZMamflXcxqtGnU7m1oIFq944wxf7kHI";
////            String getTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww0e7be2a6a0200924&corpsecret=mfL3J4oMtcksZMamflXcxqtGnU7m1oIFq944wxf7kHI";
//            ClientRequest request = new ClientRequest(getTokenUrl);
//            request.accept("application/json");
//            ClientResponse<String> response = request.get(String.class);
//            String responseContent = (String) response.getEntity();
//            logger.info("================================gettoken=============================" + responseContent + "=================================================================================");
//            net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(responseContent);
//            token = (String) json.get("access_token");
//            //获取用户信息
//            String getUserInfoUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE&agentid=AGENTID";
//            logger.info("==============================getuserinfo===============================" + getUserInfoUrl + "=================================================================================");
//
//            String UserId = "";
//            getUserInfoUrl = getUserInfoUrl.replace("ACCESS_TOKEN", token).replace("CODE", code).replace("AGENTID", "1000003");
//            request = new ClientRequest(getUserInfoUrl);
//            request.accept("application/json");
//            response = request.get(String.class);
//            responseContent = (String) response.getEntity();
//            logger.info("==============================getuserinfo_responseContent===============================" + responseContent + "=================================================================================");
//            json = net.sf.json.JSONObject.fromObject(responseContent);
//            System.out.print(json);
//            httpRequest.setAttribute("UserName", json.get("UserId"));
//            //获取用户列表
//            String url = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=" + token + "&department_id=1&fetch_child=0&status=0";
//            request = new ClientRequest(url);
//            request.accept("application/json");
//            response = request.get(String.class);
//            responseContent = (String) response.getEntity();
//            json = net.sf.json.JSONObject.fromObject(responseContent);
//            JSONArray array = JSONArray.fromObject(json.get("userlist"));
//            String users = "";
//            for (int i = 0; i < array.size(); i++) {
//                json = net.sf.json.JSONObject.fromObject(array.get(i));
//                users += "ID:" + json.get("userid") + ",name:" + json.get("name") + ",";
//            }
//            httpRequest.setAttribute("responseContent", users);
//        } catch (Exception e) {
//            e.printStackTrace();
//            logger.info("==============================Exception===============================" + e.getMessage() + "=================================================================================");
//        }
//        return "statisticalInfor";
//    }

    @RequestMapping(value = "/mapResources")
    public String mapResources() {
        return "mapResources";
    }

    @RequestMapping(value = "/eventReporting")
    public String eventReporting() {
        return "eventReporting";
    }

    @RequestMapping(value = "/statisticalInfor-facilities")
    public String statisticalInforFacilities() {
        return "statisticalInfor-facilities";
    }

    @RequestMapping(value = "/statisticalInfor-company")
    public String statisticalInforCompany() {
        return "statisticalInfor-company";
    }

    @RequestMapping(value = "/statisticalInfor-base")
    public String statisticalInforBase() {
        return "statisticalInfor-base";
    }

    @RequestMapping(value = "/statisticalInfor-vehicle")
    public String statisticalInforVehicle() {
        return "statisticalInfor-vehicle";
    }

    @RequestMapping(value = "/statisticalInfor-plan")
    public String statisticalInforPlan() {
        return "statisticalInfor-plan";
    }

    @RequestMapping(value = "/eventReporting-add")
    public String eventReportingAdd() {
        return "eventReporting-add";
    }

    @RequestMapping(value = "/statisticalInfor-company-child")
    public String statisticalInforCompanyChild(HttpServletRequest request, String t) {
        request.setAttribute("t", t);
        return "statisticalInfor-company-child";
    }

    @RequestMapping(value = "/dayMaintenance-list")
    public String dayMaintenanceList() {
        return "dayMaintenance-list";
    }

    @RequestMapping(value = "/eventReporting-list")
    public String eventReportingList() {
        return "eventReporting-list";
    }

    @RequestMapping(value = "/dayMaintenance")
    public String dayMaintenance() {
        return "dayMaintenance";
    }

    @RequestMapping(value = "/eventReporting-content")
    public String eventReportingContent() {
        return "eventReporting-content";
    }

    @RequestMapping(value = "/baiduMap")
    public String baiduMap() {
        return "baiduMap";
    }

    @RequestMapping(value = "/progress")
     public String progress() {
        return "progress";
    }

    @RequestMapping(value = "/evaluation")
    public String evaluation() {
        return "evaluation";
    }

    @RequestMapping(value = "/monitoring")
    public String monitoring() {
        return "monitoring";
    }

    @RequestMapping(value = "/panorama")
    public String panorama() {
        return "panorama";
    }

    @RequestMapping(value = "/investment")
    public String investment() {
        return "investment";
    }



    @RequestMapping(value = "/demo")
    public String demo() {
        return "demo";
    }


    public void getToken() throws Exception {
        String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww0e7be2a6a0200924&corpsecret=mfL3J4oMtcksZMamflXcxqtGnU7m1oIFq944wxf7kHI";
        ClientRequest request = new ClientRequest(url);
        request.accept("application/json");
        ClientResponse<String> response = request.get(String.class);
        String responseContent = (String) response.getEntity();
        System.out.println(responseContent);
    }

}
